{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sort the Jumbled Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sortJumbled"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将杂乱无章的数字排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>mapping</code>&nbsp;，它表示一个十进制数的映射规则，<code>mapping[i] = j</code>&nbsp;表示这个规则下将数位&nbsp;<code>i</code>&nbsp;映射为数位 <code>j</code>&nbsp;。</p>\n",
    "\n",
    "<p>一个整数 <strong>映射后的值</strong>&nbsp;为将原数字每一个数位 <code>i</code>&nbsp;（<code>0 &lt;= i &lt;= 9</code>）映射为&nbsp;<code>mapping[i]</code>&nbsp;。</p>\n",
    "\n",
    "<p>另外给你一个整数数组&nbsp;<code>nums</code>&nbsp;，请你将数组<em>&nbsp;</em><code>nums</code>&nbsp;中每个数按照它们映射后对应数字非递减顺序排序后返回。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果两个数字映射后对应的数字大小相同，则将它们按照输入中的 <strong>相对顺序</strong>&nbsp;排序。</li>\n",
    "\t<li><code>nums</code>&nbsp;中的元素只有在排序的时候需要按照映射后的值进行比较，返回的值应该是输入的元素本身。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]\n",
    "<b>输出：</b>[338,38,991]\n",
    "<b>解释：</b>\n",
    "将数字 991 按照如下规则映射：\n",
    "1. mapping[9] = 6 ，所有数位 9 都会变成 6 。\n",
    "2. mapping[1] = 9 ，所有数位 1 都会变成 8 。\n",
    "所以，991 映射的值为 669 。\n",
    "338 映射为 007 ，去掉前导 0 后得到 7 。\n",
    "38 映射为 07 ，去掉前导 0 后得到 7 。\n",
    "由于 338 和 38 映射后的值相同，所以它们的前后顺序保留原数组中的相对位置关系，338 在 38 的前面。\n",
    "所以，排序后的数组为 [338,38,991] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]\n",
    "<b>输出：</b>[123,456,789]\n",
    "<b>解释：</b>789 映射为 789 ，456 映射为 456 ，123 映射为 123 。所以排序后数组为 [123,456,789] 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>mapping.length == 10</code></li>\n",
    "\t<li><code>0 &lt;= mapping[i] &lt;= 9</code></li>\n",
    "\t<li><code>mapping[i]</code>&nbsp;的值 <strong>互不相同</strong>&nbsp;。</li>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt; 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sort-the-jumbled-numbers](https://leetcode.cn/problems/sort-the-jumbled-numbers/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sort-the-jumbled-numbers](https://leetcode.cn/problems/sort-the-jumbled-numbers/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[8,9,4,0,2,1,3,5,7,6]\\n[991,338,38]', '[0,1,2,3,4,5,6,7,8,9]\\n[789,456,123]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        mapping = [*map(str, mapping)]\n",
    "        def key_f(n):\n",
    "            ans = ''.join(mapping[i] for i in map(int, str(n)))\n",
    "            return int(ans)\n",
    "\n",
    "        nums.sort(key=key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        mapping = [*map(str, mapping)]\n",
    "        def key_f(n):\n",
    "            ans = ''.join(mapping[int(i)] for i in str(n))\n",
    "            return int(ans)\n",
    "\n",
    "        nums.sort(key=key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from string import *\n",
    "from re import *\n",
    "from datetime import *\n",
    "from collections import *\n",
    "from heapq import *\n",
    "from bisect import *\n",
    "from copy import *\n",
    "from math import *\n",
    "from random import *\n",
    "from statistics import *\n",
    "from itertools import *\n",
    "from functools import *\n",
    "from operator import *\n",
    "from io import *\n",
    "from sys import *\n",
    "from json import *\n",
    "from builtins import *\n",
    "from typing import *\n",
    "\n",
    "\"\"\"\n",
    "You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled\n",
    "decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.\n",
    "\n",
    "The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i\n",
    "in the integer with mapping[i] for all 0 <= i <= 9.\n",
    "\n",
    "You are also given another integer array nums. Return the array nums sorted in non-decreasing order\n",
    "based on the mapped values of its elements.\n",
    "\n",
    "Notes:\n",
    "\n",
    " * Elements with the same mapped values should appear in the same relative order as in the input.\n",
    " * The elements of nums should only be sorted based on their mapped values and not be replaced by\n",
    "   them.\n",
    "\n",
    " \n",
    "\n",
    "Example 1:\n",
    "\n",
    "\n",
    "Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]\n",
    "Output: [338,38,991]\n",
    "Explanation: \n",
    "Map the number 991 as follows:\n",
    "1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.\n",
    "2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.\n",
    "Therefore, the mapped value of 991 is 669.\n",
    "338 maps to 007, or 7 after removing the leading zeros.\n",
    "38 maps to 07, which is also 7 after removing leading zeros.\n",
    "Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.\n",
    "Thus, the sorted array is [338,38,991].\n",
    "\n",
    "\n",
    "Example 2:\n",
    "\n",
    "\n",
    "Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]\n",
    "Output: [123,456,789]\n",
    "Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "Constraints:\n",
    "\n",
    " * mapping.length == 10\n",
    " * 0 <= mapping[i] <= 9\n",
    " * All the values of mapping[i] are unique.\n",
    " * 1 <= nums.length <= 3 * 104\n",
    " * 0 <= nums[i] < 109\n",
    "\"\"\"\n",
    "\n",
    "MOD = 1000000007 # 998244353\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, MP: List[int], A: List[int]) -> List[int]:\n",
    "\n",
    "        def cmp(x: int):\n",
    "            return int(''.join([str(MP[int(c)]) for c in str(x)]))\n",
    "        \n",
    "        A.sort(key=cmp)\n",
    "        return A\n",
    "        \n",
    "# testcases = [\n",
    "#     [],\n",
    "#     # [],\n",
    "# ]\n",
    "\n",
    "# s = Solution()\n",
    "# func_name = dir(s)[-1]\n",
    "# func = getattr(s, func_name)\n",
    "\n",
    "# for args in testcases:\n",
    "#     print(func(*args))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def key_f(n):\n",
    "            ans = 0\n",
    "            for i in map(int, str(n)):\n",
    "                ans = ans * 10 + mapping[i]\n",
    "            return ans\n",
    "\n",
    "        nums.sort(key=key_f)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def cvt(x):\n",
    "            base = 1\n",
    "            res = 0\n",
    "            while True:\n",
    "                res += mapping[x%10] * base\n",
    "                x //= 10\n",
    "                base *= 10\n",
    "                if x == 0:\n",
    "                    break\n",
    "            return res\n",
    "        nums.sort(key=cvt)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "\n",
    "        mapping = {str(i):str(j) for i,j in enumerate(mapping)}     # mapping 字符串\n",
    "        print(mapping)\n",
    "        def trans(x):       # 根据规则将x转为对应的数字\n",
    "            news = ''\n",
    "            for ch in str(x):\n",
    "                news += mapping[ch]\n",
    "            return int(news)\n",
    "\n",
    "        # 主程序\n",
    "        nums.sort(key = lambda x: trans(x))\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        mapping = [str(i)for i in mapping]\n",
    "        def fn(num):\n",
    "            res = ''\n",
    "            for i in str(num):\n",
    "                res+=mapping[int(i)]\n",
    "            return int(res)\n",
    "        nums.sort(key=fn)\n",
    "        return nums\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        # 制作翻译表\n",
    "        tab = str.maketrans(\"0123456789\", \"\".join(map(str, mapping)))\n",
    "        # 自定义排序: (num2map,idx)\n",
    "        return sorted(nums, key=lambda x: int(str(x).translate(tab)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def get_mapped_num(num: int) -> int:\n",
    "            digits = list(map(int, str(num)))  # 将每一个数拆解成各个数位\n",
    "            mapped_digits = [mapping[digit] for digit in digits]  # 应用映射规则\n",
    "            return int(''.join(map(str, mapped_digits)))  # 将映射后的各数位组合成一个整数\n",
    "        \n",
    "        # 使用 sorted 函数和自定义的 key 函数进行排序\n",
    "        return sorted(nums, key=get_mapped_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        tab = str.maketrans(\"0123456789\", \"\".join(map(str, mapping)))\n",
    "        return sorted(nums, key=lambda x: int(str(x).translate(tab)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        # 制作翻译表\n",
    "        tab = str.maketrans(\"0123456789\", \"\".join(map(str, mapping)))\n",
    "        # 自定义排序: (num2map,idx)\n",
    "        return sorted(nums, key=lambda x: int(str(x).translate(tab)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def f(x):\n",
    "            if x == 0:\n",
    "                return mapping[0]\n",
    "            ans = 0\n",
    "            i = 0\n",
    "            while x:\n",
    "                ans += 10 ** i * mapping[x%10]\n",
    "                x //= 10\n",
    "                i += 1\n",
    "            return ans\n",
    "        \n",
    "        return sorted(nums, key=f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        tab = str.maketrans('0123456789', ''.join(map(str, mapping)))\n",
    "        return sorted(nums, key = lambda x: int(str(x).translate(tab)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def mapped_value(num: int) -> int:\n",
    "            return int(''.join(str(mapping[int(digit)]) for digit in str(num)))\n",
    "\n",
    "        return sorted(nums, key=mapped_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        return sorted(nums, key=lambda x: int(''.join(str(mapping[d]) for d in map(int, str(x)))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def trans(x):\n",
    "            ret = ''\n",
    "            for c in str(x):\n",
    "                k = ord(c) - ord('0')\n",
    "                ret += str(mapping[k])\n",
    "            return int(ret)\n",
    "        # print([trans(x) for x in nums])\n",
    "        idx = list(range(len(nums)))\n",
    "        idx.sort(key=lambda i: trans(nums[i]))\n",
    "        return [nums[i] for i in idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import *\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        map_func = lambda x : str(mapping[int(x)])\n",
    "        jumbled_nums = [ int( ''.join(list( map( map_func, list(str(num)))\n",
    "                            ))) for num in nums ]\n",
    "        \n",
    "        idx_ls = list(range(len(nums)))\n",
    "        idx_ls.sort(\n",
    "            key=lambda x : jumbled_nums[x]\n",
    "        )\n",
    "\n",
    "        return [ nums[idx] for idx in idx_ls ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        return_list =[]\n",
    "        for number in nums:\n",
    "            num_str = str(number)\n",
    "            return_str =\"\"\n",
    "            for id in num_str:\n",
    "                return_str =return_str+str(mapping[int(id)])\n",
    "            return_list.append(int(return_str))\n",
    "\n",
    "        sorted_indices = sorted(range(len(return_list)), key=lambda k: return_list[k])\n",
    "\n",
    "        nums = [nums[i] for i in sorted_indices]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        return_list =[]\n",
    "        for number in nums:\n",
    "            num_str = str(number)\n",
    "            return_str =\"\"\n",
    "            for id in num_str:\n",
    "                return_str =return_str+str(mapping[int(id)])\n",
    "            return_list.append(int(return_str))\n",
    "\n",
    "        sorted_indices = sorted(range(len(return_list)), key=lambda k: return_list[k])\n",
    "\n",
    "        nums = [nums[i] for i in sorted_indices]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x):\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x))) #一句话就完事了\n",
    "        \n",
    "        num_zip = sorted([(num, transfer(num)) for num in nums], key = lambda x: x[1])\n",
    "        return [num for (num, num_transfer) in num_zip]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def getMappedNum(num:int) -> int:\n",
    "            return int(''.join(str(mapping[int(ch)]) for ch in str(num)))\n",
    "\n",
    "        return [v2 for (v1,v2) in sorted([(getMappedNum(num) , num) for num in nums] , key=lambda e:e[0])]    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x: int) -> int:\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x)))\n",
    "\n",
    "        num_pairs = [(transfer(num), num) for num in nums]\n",
    "        num_pairs.sort(key=lambda pair: pair[0])\n",
    "\n",
    "        ans = [num for (_, num) in num_pairs]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x: int) -> int:\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x)))\n",
    "        \n",
    "        num_pairs = [(transfer(num), num) for num in nums]\n",
    "        num_pairs.sort(key=lambda pair: pair[0])\n",
    "        \n",
    "        ans = [num for (_, num) in num_pairs]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x: int) -> int:\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x)))\n",
    "        \n",
    "        num_pairs = [(transfer(num), num) for num in nums]\n",
    "        num_pairs.sort(key=lambda pair: pair[0])\n",
    "        \n",
    "        ans = [num for (_, num) in num_pairs]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x: int) -> int:\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x)))\n",
    "        \n",
    "        num_pairs = [(transfer(num), num) for num in nums]\n",
    "        num_pairs.sort(key=lambda pair: pair[0])\n",
    "        \n",
    "        ans = [num for (_, num) in num_pairs]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transform(num:int)->int:\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(num)))\n",
    "        num_pairs=[(transform(num),num) for num in nums]\n",
    "        num_pairs.sort(key=lambda x:(x[0]))\n",
    "        ans=[num for (_,num) in num_pairs]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 自定义排序\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x: int) -> int:\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x)))\n",
    "        \n",
    "        num_pairs = [(transfer(num), num) for num in nums]\n",
    "        num_pairs.sort(key=lambda pair: pair[0])\n",
    "        \n",
    "        ans = [num for (_, num) in num_pairs]\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        arr = []\n",
    "        result=[]\n",
    "        for i in range(len(nums)):\n",
    "            t = 0;\n",
    "            sum = 0\n",
    "            s=nums[i]\n",
    "            if nums[i]==0:\n",
    "                arr.append([nums[i],mapping[0]])\n",
    "            else:\n",
    "                while s>= 1:\n",
    "                    sum += mapping[s % 10] * 10 ** t\n",
    "                    s //= 10\n",
    "                    t+=1\n",
    "                arr.append([nums[i],sum])\n",
    "        arr=sorted(arr,key=lambda d:d[1])\n",
    "        for i in arr:\n",
    "            result.append(i[0])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def transfer(x):\n",
    "            return int(\"\".join(str(mapping[int(digit)]) for digit in str(x)))\n",
    "        num_pairs=[(transfer(num),num) for num in nums]\n",
    "        num_pairs.sort(key=lambda x: x[0])\n",
    "        ans=[n[1] for n in num_pairs]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        num=[i for i in range(10)]\n",
    "        dic=dict(zip(num,mapping))\n",
    "        num_change=[0]*len(nums)\n",
    "        new_nums=[]\n",
    "        for i in range(len(nums)):\n",
    "            number=str(nums[i])\n",
    "            l=[]\n",
    "            for n in number:\n",
    "                n=int(n)\n",
    "                new_n=dic[n]\n",
    "                l.append(str(new_n))\n",
    "            new_number=int(''.join(l))\n",
    "            new_nums.append(new_number)\n",
    "        q=list(zip(new_nums,nums))\n",
    "        q.sort(key=lambda x:x[0])\n",
    "        return [i[1] for i in q]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        num_idx=[]\n",
    "        for i in range(len(nums)):\n",
    "            num=str(nums[i])\n",
    "            t=''\n",
    "            for c in num:\n",
    "                t+=str(mapping[int(c)])\n",
    "            num_idx.append(int(t))\n",
    "        cnum=list(zip(num_idx,nums))\n",
    "        cnum.sort(key=lambda x:x[0])\n",
    "        res=[x[1] for x in cnum]\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        tmp = []\n",
    "        for num in nums:\n",
    "            s = str(num)\n",
    "            t = 0\n",
    "            for i in range(len(s)):\n",
    "                t = t*10 + mapping[int(s[i])]\n",
    "            tmp.append(t)\n",
    "        ans = []\n",
    "        for num,t in zip(nums,tmp):\n",
    "            ans.append([num,t])\n",
    "        ans.sort(key=lambda x : x[1])\n",
    "        res = []\n",
    "        for num,t in ans:\n",
    "            res.append(num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "       n = len(nums)\n",
    "       arr = [0] * n\n",
    "       for i, v in enumerate(nums):\n",
    "          sum = 0\n",
    "          s = str(v)\n",
    "          for j in range(len(s)):\n",
    "             sum = sum * 10 + mapping[int(s[j])]\n",
    "          arr[i] = sum\n",
    "       res = []\n",
    "       for x, _ in sorted(zip(nums, arr), key=lambda k: k[1]):\n",
    "          res.append(x)\n",
    "       return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        num_idx=[]\n",
    "        for i in range(len(nums)):\n",
    "            num=str(nums[i])\n",
    "            t=''\n",
    "            for c in num:\n",
    "                t+=str(mapping[int(c)])\n",
    "            num_idx.append(int(t))\n",
    "        cnum=list(zip(num_idx,nums))\n",
    "        cnum.sort(key=lambda x:x[0])\n",
    "        res=[x[1] for x in cnum]\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        num_idx=[]\n",
    "        for i in range(len(nums)):\n",
    "            num=str(nums[i])\n",
    "            t=''\n",
    "            for c in num:\n",
    "                t+=str(mapping[int(c)])\n",
    "            num_idx.append(int(t))\n",
    "        cnum=list(zip(num_idx,nums))\n",
    "        cnum.sort(key=lambda x:x[0])\n",
    "        res=[x[1] for x in cnum]\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        arr = []\n",
    "        res = []\n",
    "        def convert(mapping, num):\n",
    "            digit = 1\n",
    "            ret = 0\n",
    "            if (num <= 9):\n",
    "                return mapping[num]\n",
    "            while num:\n",
    "                mod = num % 10\n",
    "                ret += digit * mapping[mod]\n",
    "                digit *= 10\n",
    "                num //= 10\n",
    "            return ret\n",
    "        for i in nums:\n",
    "            temp = []\n",
    "            temp.append(i)\n",
    "            temp.append(convert(mapping, i))\n",
    "            arr.append(temp)\n",
    "        arr.sort(key = lambda x : x[1])\n",
    "        for i in range(len(nums)):\n",
    "            res.append(arr[i][0])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        \n",
    "        def f(num):\n",
    "            if num == 0:\n",
    "                return mapping[0]\n",
    "            ans = 0\n",
    "            i = 0\n",
    "            while num:\n",
    "                x = num % 10\n",
    "                ans = ans+mapping[x]*(10**i)\n",
    "                num = (num-x)//10\n",
    "                i += 1\n",
    "            return ans\n",
    "       \n",
    "        tmp = [(f(nums[i]), i, nums[i]) for i in range(len(nums))]\n",
    "        tmp.sort()\n",
    "      \n",
    "        return [n[2] for n in tmp]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ind = list(range(n))\n",
    "        ind.sort(key=lambda it: [int(\"\".join(str(mapping[int(w)]) for w in str(nums[it]))), it])\n",
    "        return [nums[i] for i in ind]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        nums2 = []\n",
    "        for i in range(len(nums)):\n",
    "            l = list(str(nums[i]))\n",
    "            num = ''\n",
    "            for j in range(len(l)):\n",
    "                num = num + str(mapping[int(l[j])])\n",
    "            nums2.append(int(num))\n",
    "        sorted_zip = sorted(zip(nums, nums2), key=lambda x:x[1])\n",
    "        nums, nums2 = zip(*sorted_zip)\n",
    "        return list(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        m = {str(i):str(x) for i, x in enumerate(mapping)}\n",
    "        l = [[x, y] for x, y in zip(nums, range(n))]\n",
    "        for x in l:\n",
    "            t = list(str(x[0]))\n",
    "            for i,y in enumerate(t):\n",
    "                t[i] = m[y]\n",
    "            x[0] = int(\"\".join(t))\n",
    "        l.sort()\n",
    "        ans = []\n",
    "        for x in l:\n",
    "            ans.append(nums[x[1]])\n",
    "        return ans\n",
    "                \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        Res = []\n",
    "        D = {}\n",
    "        for idx,i in enumerate(mapping):\n",
    "            D[str(idx)] = str(i)\n",
    "        for i in nums:\n",
    "            res = ''\n",
    "            s = str(i)\n",
    "            for x in s:\n",
    "                res += D[x]\n",
    "            Res.append(int(res))\n",
    "        L = [[a,b] for a,b in zip(nums,Res)]\n",
    "        L.sort(key = lambda x:x[1])\n",
    "        res = []\n",
    "        for i in L:\n",
    "            res.append(i[0])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def f(x):\n",
    "            s = str(x)\n",
    "            s1 = ''.join(str(mapping[int(c)]) for c in s)\n",
    "            return int(s1)\n",
    "        \n",
    "        locations = {x:i for i, x in enumerate(nums)}\n",
    "        nums.sort(key=lambda x:[f(x), locations[x]])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        hp = []\n",
    "\n",
    "        for n in nums:\n",
    "            ss = str(n)\n",
    "            a = []\n",
    "            for s in ss:\n",
    "                a.append(str(mapping[int(s)]))\n",
    "            \n",
    "            hp.append(int(''.join(a)))\n",
    "\n",
    "        \n",
    "\n",
    "        return [ele for ele,_, _ in sorted(zip(nums, hp, range(len(nums))), key = lambda x:(x[1], x[2]))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        \n",
    "        def f(num):\n",
    "            if num == 0:\n",
    "                return mapping[0]\n",
    "            ans = 0\n",
    "            i = 0\n",
    "            while num:\n",
    "                x = num % 10\n",
    "                ans = ans+mapping[x]*(10**i)\n",
    "                num = (num-x)//10\n",
    "                i += 1\n",
    "            return ans\n",
    "        print(f(1))\n",
    "        tmp = [(f(nums[i]), i, nums[i]) for i in range(len(nums))]\n",
    "        tmp.sort()\n",
    "        print(tmp)\n",
    "        return [n[2] for n in tmp]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def get(v):\n",
    "            ans = []\n",
    "            for c in str(v):\n",
    "                ans.append(str(mapping[int(c)]))\n",
    "            return int(''.join(ans))\n",
    "        res = [[get(nums[i]),i] for i in range(len(nums))]\n",
    "        #print(res)\n",
    "        res.sort(key = lambda x:(x[0],x[1]))\n",
    "        return [nums[res[i][1]] for i in range(len(res))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        cnt = defaultdict(list)\n",
    "        for x in nums:\n",
    "            x = str(x)\n",
    "            s = ''\n",
    "            for c in x:\n",
    "                s += str(mapping[int(c)])\n",
    "            cnt[int(s)].append(int(x))\n",
    "        res = [k for k in cnt.keys()]\n",
    "        res.sort()\n",
    "        ans = []\n",
    "        for x in res:\n",
    "            ans.extend(cnt[x])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def getMappedDic(mapping: List[int], nums: List[int]) -> dict[str,List[int]]:\n",
    "            result = {}\n",
    "            for num in nums:\n",
    "                numStr = str(num)\n",
    "                mapStr = ''\n",
    "                for ch in numStr :\n",
    "                    mapStr += str(mapping[int(ch)])\n",
    "                mapStrKey = int(mapStr)\n",
    "                if result.get(mapStrKey):\n",
    "                   result[mapStrKey].append(num)\n",
    "                else:\n",
    "                   result[mapStrKey] = [num]  \n",
    "            return result\n",
    "\n",
    "        items = sorted(getMappedDic(mapping , nums).items())\n",
    "        result = []\n",
    "        for k , v in items:\n",
    "            result.extend(v)\n",
    "        return result\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        def getMappedDic(mapping: List[int], nums: List[int]) -> dict[str,List[int]]:\n",
    "            result = {}\n",
    "            for num in nums:\n",
    "                numStr = str(num)\n",
    "                mapStr = ''\n",
    "                for ch in numStr :\n",
    "                    mapStr += str(mapping[int(ch)])\n",
    "                mapStrKey = int(mapStr)\n",
    "                if result.get(mapStrKey):\n",
    "                   result[mapStrKey].append(num)\n",
    "                else:\n",
    "                   result[mapStrKey] = [num]  \n",
    "            return result\n",
    "\n",
    "        result = []\n",
    "        mappedDic = getMappedDic(mapping , nums)\n",
    "        items = sorted(mappedDic.items())\n",
    "        for k , v in items:\n",
    "            result.extend(v)\n",
    "        return result\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        strMap = [str(num) for num in mapping]\n",
    "        result = []\n",
    "        for i, num in enumerate(nums):\n",
    "            tmp = ''\n",
    "            for c in str(num):\n",
    "                tmp += strMap[int(c)]\n",
    "            result.append([num,int(tmp),i])\n",
    "        result.sort(key = lambda x:(x[1],x[2]))\n",
    "        return [num for num,_,_ in result]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "        strMap = [str(num) for num in mapping]\n",
    "        result = []\n",
    "        for i, num in enumerate(nums):\n",
    "            tmp = ''\n",
    "            for c in str(num):\n",
    "                tmp += strMap[int(c)]\n",
    "            result.append([num,int(tmp),i])\n",
    "        result.sort(key = lambda x:(x[1],x[2]))\n",
    "        return [num for num,_,_ in result]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n",
    "\n",
    "        def f(x):\n",
    "            L = list(str(x))\n",
    "            ln = len(L)\n",
    "            for i in range(ln):\n",
    "                L[i] = str(mapping[int(L[i])])\n",
    "            return int(''.join(L))\n",
    "        \n",
    "        res = []\n",
    "        for i, j in enumerate(nums):\n",
    "            res.append([j, i])\n",
    "        res.sort(key=lambda p: [f(p[0]), p[1]])\n",
    "        return [x[0] for x in res]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
